<?php
// test_email.php
// 用于测试SMTP邮件发送功能

session_start();

// 检查是否已登录
if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
    header('Location: index.php');
    exit;
}

// 引入发送邮件函数
require 'send_email.php';

$success = false;
$error = '';
$email_sent = false;

// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $to_qq = $_POST['to_qq'] ?? '';
    $subject = $_POST['subject'] ?? '';
    $message = $_POST['message'] ?? '';

    if (empty($to_qq) || empty($subject) || empty($message)) {
        $error = '请填写所有字段';
    } elseif (!is_numeric($to_qq)) {
        $error = 'QQ号码必须是数字';
    } else {
        // 发送测试邮件
        $success = send_email_by_qq($to_qq, $subject, $message);
        if ($success) {
            $email_sent = true;
        } else {
            $error = '邮件发送失败，请检查SMTP设置';
        }
    }
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>✿ 测试邮件发送 ✿</title>
    <link rel="icon" href="favicon.ico" type="image/x-icon">
    <link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=ZCOOL+KuaiLe&family=Ma+Shan+Zheng&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="styles_anime_admin.css">
    <script src="https://cdn.jsdelivr.net/npm/three@0.132.2/build/three.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/gsap@3.11.3/dist/gsap.min.js"></script>
    <style>
        .test-container {
            max-width: 600px;
            margin: 30px auto;
            padding: 25px;
            background-color: var(--surface);
            border-radius: 24px;
            box-shadow: 0 10px 30px rgba(255, 107, 139, 0.2);
            position: relative;
            overflow: hidden;
            border: 3px solid var(--border);
            backdrop-filter: blur(10px);
            -webkit-backdrop-filter: blur(10px);
        }
        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
            padding-bottom: 15px;
            border-bottom: 1px solid #eee;
        }
        .header h1 {
            margin: 0;
            position: relative;
        }
        .header h1::before,
        .header h1::after {
            content: '✿';
            color: var(--secondary);
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            font-size: 1.5rem;
        }
        .header h1::before {
            left: -30px;
        }
        .header h1::after {
            right: -30px;
        }
        .back-btn {
            padding: 10px 20px;
            background-color: var(--secondary);
            color: white;
            border: none;
            border-radius: 50px;
            cursor: pointer;
            text-decoration: none;
            font-size: 1rem;
            transition: all 0.3s ease;
            position: relative;
            overflow: hidden;
            box-shadow: 0 4px 8px rgba(138, 92, 242, 0.3);
        }
        .back-btn:hover {
            background-color: var(--secondary-light);
            transform: translateY(-2px);
            box-shadow: 0 6px 12px rgba(138, 92, 242, 0.4);
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
            color: var(--primary);
            font-size: 1.1rem;
        }
        .form-group input,
        .form-group textarea {
            width: 100%;
            padding: 12px;
            border: 2px solid var(--border);
            border-radius: 12px;
            background-color: rgba(255, 255, 255, 0.7);
            font-size: 1rem;
            transition: all 0.3s ease;
        }
        .form-group textarea {
            height: 150px;
            resize: vertical;
        }
        .form-group input:focus,
        .form-group textarea:focus {
            border-color: var(--primary);
            outline: none;
            box-shadow: 0 0 10px rgba(255, 107, 139, 0.2);
        }
        .btn-send {
            padding: 12px 25px;
            background-color: var(--primary);
            color: white;
            border: none;
            border-radius: 50px;
            cursor: pointer;
            font-size: 1rem;
            transition: all 0.3s ease;
            position: relative;
            overflow: hidden;
            box-shadow: 0 4px 8px rgba(255, 107, 139, 0.3);
        }
        .btn-send:hover {
            background-color: var(--primary-light);
            transform: translateY(-2px);
            box-shadow: 0 6px 12px rgba(255, 107, 139, 0.4);
        }
        .success-message {
            color: var(--success);
            margin-bottom: 15px;
            font-weight: bold;
            text-align: center;
            padding: 10px;
            border-radius: 12px;
            background-color: rgba(149, 225, 125, 0.2);
            border: 2px solid var(--success-border);
        }
        .error-message {
            color: var(--error);
            margin-bottom: 15px;
            font-weight: bold;
            text-align: center;
            padding: 10px;
            border-radius: 12px;
            background-color: rgba(231, 76, 60, 0.2);
            border: 2px solid var(--error-border);
        }
    </style>
</head>
<body>
    <div class="test-container">
        <div class="header">
            <div class="anime-decoration"></div>
            <h1>测试邮件发送</h1>
            <a href="smtp_settings.php" class="back-btn">
                <span>返回SMTP设置</span>
                <span class="heart">♡</span>
            </a>
        </div>

        <?php if ($email_sent): ?>
            <div class="success-message">邮件发送成功！</div>
        <?php endif; ?>

        <?php if (!empty($error)): ?>
            <div class="error-message"><?php echo $error; ?></div>
        <?php endif; ?>

        <form method="POST" action="test_email.php">
            <div class="form-group">
                <label for="to_qq">收件人QQ号</label>
                <input type="text" id="to_qq" name="to_qq" placeholder="输入QQ号码" required>
            </div>
            <div class="form-group">
                <label for="subject">邮件主题</label>
                <input type="text" id="subject" name="subject" placeholder="输入邮件主题" required>
            </div>
            <div class="form-group">
                <label for="message">邮件内容</label>
                <textarea id="message" name="message" placeholder="输入邮件内容" required></textarea>
            </div>
            <button type="submit" class="btn-send">
                <span>发送测试邮件</span>
                <span class="heart">♡</span>
            </button>
        </form>
    </div>
<script src="script_anime_admin.js"></script>
</body>
</html>